package ui;
import Pojo.Book;
import Pojo.Reader;
import Service.LibraryService;
import Service.ReaderService;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
class ReaderManager extends JInternalFrame implements ActionListener {
    private JTextField txtName;
    private JTextField txtCode;
    private JTable table;
    private JTextField txtSex;
    private JTextField txtSub;
    private DefaultTableModel dtm;

    private ReaderService readerService = new ReaderService();
    public ReaderManager() {
        super("读者管理", true, true, true, true);
        setBounds(100, 100, 678, 450);

        JPanel panelTop = new JPanel();
        getContentPane().add(panelTop, BorderLayout.NORTH);

        JLabel labelCode = new JLabel("学号");
        panelTop.add(labelCode);
        txtCode = new JTextField();
        txtCode.setColumns(10);
        panelTop.add(txtCode);

        JLabel labelName = new JLabel("姓名");
        panelTop.add(labelName);
        txtName = new JTextField();
        txtName.setColumns(10);
        panelTop.add(txtName);

        JLabel labelSex = new JLabel("性别");
        panelTop.add(labelSex);
        txtSex = new JTextField();
        txtSex.setColumns(10);
        panelTop.add(txtSex);

        JLabel labelSub = new JLabel("学院");
        panelTop.add(labelSub);
        txtSub = new JTextField();
        txtSub.setColumns(10);
        panelTop.add(txtSub);

        JButton btnQuery = new JButton("查询");
        btnQuery.addActionListener(this);
        panelTop.add(btnQuery);

        JScrollPane scrollPane = new JScrollPane();
        getContentPane().add(scrollPane, BorderLayout.CENTER);
        String[] header = {"学号", "姓名", "性别", "年龄", "学院"};

        dtm = new DefaultTableModel(null, header);
        table = new JTable(dtm) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }

        };
        table.setRowHeight(35);
        scrollPane.setViewportView(table);

        JPanel panelBottom = new JPanel();
        getContentPane().add(panelBottom, BorderLayout.SOUTH);

        JButton btnDel = new JButton("删除读者");
        btnDel.addActionListener(this);
        panelBottom.add(btnDel);

        JButton btnEdit = new JButton("编辑读者信息");
        btnEdit.addActionListener(this);
        panelBottom.add(btnEdit);

        JButton btnFind = new JButton("查看读者信息");
        btnFind.addActionListener(this);
        panelBottom.add(btnFind);

        JButton btnAdd = new JButton("添加读者");
        btnAdd.addActionListener(this);
        panelBottom.add(btnAdd);
        loadReader();
        setVisible(true);
    }
    public void loadReader() {
        String code = txtCode.getText();
        String name = txtName.getText();
        String sex = txtSex.getText();
        String sub = txtSub.getText();
        dtm.setRowCount(0);
        List<Reader> list = readerService.findReaderAll(null, null, null,null,null);

        String[][] data = readerService.listToArray(list); //List<Reader> list
        for (int i = 0; i < data.length; i++) {
            dtm.addRow(data[i]);
        }
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("删除读者")) {
            int index = table.getSelectedRow();
            if (index != -1) {
                int result = JOptionPane.showConfirmDialog(this, "确定删除？", "系统 提示", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
                if (result == JOptionPane.YES_OPTION) {
                    ReaderService.delReader(index);
                    JOptionPane.showMessageDialog(this, "删除成功！");
                    loadReader();
                }
            } else {
                JOptionPane.showMessageDialog(this, "请选择需要删除的行！");
                loadReader();
            }
        }else if (e.getActionCommand().equals("编辑读者信息")){
            int index = table.getSelectedRow();
            if (index != -1){
                Reader reader = ReaderService.findReaderById(index);
                new ReaderEditManger(reader,this);

            }else{
                JOptionPane.showMessageDialog(this, "请选择需要编辑的行！");
                loadReader();
            }
        }else if(e.getActionCommand().equals("查看读者信息")){
            int index = table.getSelectedRow();
            if (index!=-1){
                Reader reader=ReaderService.findReaderById(index);
                new ReaderCheckManger(reader,this);
            }else{
                JOptionPane.showMessageDialog(this,"请选择需要查看的信息！");
                loadReader();
            }

        }else if(e.getActionCommand().equals("添加读者信息")){
            new ReaderAddManger();
        }
    }
}
